Since 2015, JavaScript has improved immensely.
It’s much more pleasant to use it now than ever.
In this article, we’ll look at some JavaScript promise methods.
finally
The finally
method lets us run code in a promise chain regardless of the outcome of the promise chain.
For instance, we can write:
asyncFn()
.then(function(value1) {
//...
})
.then(function(value2) {
//...
})
.finally(function() {
// ...
});
We call finally
so that we can run a callback that runs regardless of the outcome.
This is useful for running clean up code and something like that.
ES6 Promise Libraries
There’re many promise libraries we can use.
Many of them conform to the ES6 API.
They include:
- “ES6-Promises — small ES6 promise polyfill
- “Native Promise Only (NPO) — small ES6 promise polyfill
- “Lie” — small ES6 promise polyfill
- “RSVP.js” — larger promise library
- “Bluebird” — larger promise library
- Q.Promise — larger promise library
- “ES6 Shim” — standard polyfill
- “core-js” — standard polyfill
ES6 Promise API
The ES6 promise API has various parts.
The most basic part is the Promise
constructor.
It takes a callback that has the resolve
and reject
functions as parameters.
resolve
resolves the promise with the passed in value.
It’ll be forwarded to the then
callback if then
is called.
reject
rejects the promise with the given value.
The argument of reject
is usually an Error
instance.
We can use it by writing:
const p = new Promise(function(resolve, reject) {
//...
});
Static Promise
Methods
There’re several static promise methods in the API.
They include the Promise.resolve
and Promise.reject
methods.
Promise.resolve
converts an arbitrary value to a promise.
We pass in an argument to it to return a value with the resolved value.
We can call Promise.resolve
with:
Promise.resolve(1)
Promise.reject
lets us create a new promise that’s rejected with a reason.
We can call Promise.reject
with:
Promise.reject(1)
There’re several methods to compose promises.
Promise.all
and Promise.race
lets us run multiple promises in different ways.
Promise.all
takes an iterable and is fulfilled if all elements in the array of promises are fulfilled.
If any of the promises in the iterable are rejected, then the rejection value is the first rejection value from the promises.
Promise.race
also takes an iterable which settles the returned promise.
It’ll return a promise that resolves to the value that’s first resolved.
Promise Instance M
ethods
Promise instance has various methods.
The Prototype.prototype.then(onFulfilled, onRejected)
method takes a onFulfilled
and onRejected
callbacks.
onFulfilled
is called immediately when the promise is resolved.
onRejected
is called when the promise is rejected.
then
returns a new promise that’s resolved to the value returned in the promise.
If the then
callback throws an exception, then the promise returned is rejected.
If onFulfilled
is omitted, then the fulfilled result is forwarded to the next then
.
If onRejected
is omitted, the rejection is forwarded to the next then
.
Promise.prototype.catch(onRejected)
lets us catch the error of the first rejected promise.
It’s the same as p.then(null, onRejected)
.
Conclusion
The Promise API is a standard API that lets us write async code easily.
They can be chained and we can catch errors with them.